<template>
  <el-container class="Databases">
    <el-main class="minScrollBarStyle" ref="containerRef">
      <div class="location">
        <div class="location-title">您当前的地理位置：{{ position }}</div>
        <div class="location-suggestion">已根据您的地理位置，采取推优住宿推荐</div>
      </div>
      <!-- 滚动按钮 -->
      <!-- v-bind="getScrollInfo" -->
      <getScroll></getScroll>

      <el-row :gutter="50">
        <el-col
          :xs="24"
          :sm="24"
          :md="24"
          :lg="24"
          :xl="24"
          v-for="(item, index) in accommodations.accommodationLists"
          :key="index"
        >
          <el-card class="box-card animalCard" shadow="hover" @click="selectAccommodation(item)">
            <div class="card">
              <!-- <div class="blob"></div> -->
              <!-- @click="toInfoParticulars(item.animalId)" -->
              <div class="card-left">
                <el-image :src="item.images[0]" class="img" :lazy="true"></el-image>
              </div>
              <div class="card-right">
                <h2>{{ item.name }}</h2>
                <span class="info"
                  >{{ item.house_type }} | {{ item.rental_type }} |
                  {{ "宜居" + item.people_ceiling + "人" }}</span
                >
                <div class="local">
                  <el-icon>
                    <LocationFilled />
                  </el-icon>
                  <span>地址：{{ item.location }}</span>
                </div>
                <div class="charge">
                  <span>{{ "￥ " + item.charge }}</span>
                </div>
                <div class="more">点击获取更多信息</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 分页 -->
      <ele-pagination
        :totalCount="accommodations.accommodationTotal"
        styleClass="common"
        @paginationChange="paginationChange"
      ></ele-pagination>
    </el-main>
  </el-container>
</template>

<script setup lang="ts">
import elePagination from "@/components/ele-pagination/ele-pagination.vue"
import getScroll from "@/components/UI-cnps/getScroll/getScroll.vue"
import useRouterJump from "@/hooks/useRouterJump"
// import type { IAnimalType } from "@/types"
import { localCache } from "@/utils/cacheData"
import { storeToRefs } from "pinia"
import { onMounted, onUnmounted, ref, watch } from "vue"
import { messageReminder } from "@/utils/notificationHint"
import useAccommodationStore from "@/store/main/acccommodation/acccommodation"
import { useRoute } from "vue-router"
// import type { IGetScrollInfo } from "@/components/UI-cnps/getScroll/type"
// 获取滚动区域的dom
const containerRef = ref<Element>()

/* 获取数据 */
function getAccommodation(limit?: number, offset?: number) {
  return new Promise<void>((resolve, reject) => {
    accommodationStore
      .getAccommodation(limit, offset)
      .then((res) => {
        resolve(res)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

// 3.路由跳转 数据量太大，用路由传参方式不合适
const { routerJump } = useRouterJump()

// 5.放到localStorage
function selectAccommodation(item) {
  localCache.setCache("accommodationInfo", item)
  routerJump("push", "AccommodationParticular")
}

/* 分页器的自定义事件 */
function paginationChange(limit, offset) {
  getAccommodation(limit, offset)
}

const accommodationStore = useAccommodationStore()
const { accommodations } = storeToRefs(accommodationStore)

const route = useRoute()
watch(
  () => route.query.keyWord,
  (newValue, oldValue) => {
    // 检查新旧值是否相同
    if (newValue !== oldValue) {
      if (!newValue) {
        // 执行没有关键字参数时的处理逻辑
        getAccommodation()
      } else {
        // 执行有关键字参数时的处理逻辑
        accommodationStore
          .likeAccommodation(newValue, 20, 0)
          .then((res) => {
            if (!res.total) {
              // 重新加载默认数据
              routerJump("push", "Databases")
              // 处理关键字不存在的情况
              messageReminder({
                message: "暂无该关键字的信息",
                type: "error"
              })
            }
          })
          .catch((error) => {
            console.error("Error searching accommodation:", error)
            // 可以选择继续路由导航或者中断导航
          })
      }
    }
  }
)
onMounted(() => {
  if (!route.query.keyWord) {
    // 挂载时默认获取一次动物的数据
    getAccommodation()
  } else {
    accommodationStore.likeAccommodation(route.query.keyWord, 20, 0).then((res) => {
      if (!res.total) {
        messageReminder({
          message: "暂无改关键字的信息",
          type: "error"
        })
        getAccommodation()
      }
    })
  }
})
</script>

<style lang="less" scoped>
.location {
  font-size: 22px;
  color: #888;
  margin-top: 15px;
  .location-title {
    display: flex;
    justify-content: center;
  }
  .location-suggestion {
    display: flex;
    justify-content: center;
    margin-top: 8px;
  }
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
}
.el-main {
  --el-main-padding: 0 160px;
}
// ++++++++++++++++++++++++
.el-row {
  .box-card {
    width: 800px;
    margin: 26px auto 0 auto;
    border-radius: 10px;
    position: relative;
    // overflow: visible;

    // 卡片start
    .card {
      width: 100%;
      // height: 254px;
      margin: 0 auto;
      border-radius: 10px;
      transition: all 0.5s;
      // 水滴有下边距 图片有上边距 消除外边距合并、
      overflow: hidden;
      border-bottom: 1px solid #eee;
      display: flex;

      .card-left {
        width: 40%;
        .img {
          cursor: pointer;
          display: flex;
          width: 320px;
          height: 260px;
          font-size: 10px;
        }
      }

      .card-right {
        width: 60%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        h2 {
          padding: 15px 10px;
          font-size: 24px;
          font-weight: 500;
          transition: all 0.1s;
          line-height: 16px;
          transition: all 0.5s;
          width: 80%;
          overflow: hidden;
          white-space: nowrap; /* 不换行 */
          text-overflow: ellipsis; /* 显示省略号 */
        }
        .info {
          color: #888888;
        }
        .local {
          width: 80%;
          margin-top: 10px;
          overflow: hidden;
          white-space: nowrap; /* 不换行 */
          text-overflow: ellipsis; /* 显示省略号 */
          .el-icon {
            font-size: 16px;
            margin-right: 6px;
            color: #fe7e71;
          }
          span {
            color: #999;
          }
        }
        .charge {
          padding: 20px 0;
          font-size: 36px;
          color: #fb8477;
        }
        .more {
          color: #888;
        }
      }
    }
    // 用户信息卡片end
  }
}

:deep(.el-card__body) {
  padding: 0px;
}

.el-tag {
  font-size: 14px;
  margin-right: 5px;
  margin-top: 8px;
}
</style>

<style>
/* vue一个页面中突然因为某些原因出现了滚动条，但是因为HTML产生的，怎么在该页面设置不然HTML有滚动条产生 */
/* 
// html {
//   overflow: hidden;
// }
> 使用js进行控制
*/
</style>
